package com.carlosk.kandicar.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.carlosk.kandicar.R;
import com.carlosk.kandicar.ui.base.BaseAC;
import com.carlosk.kandicar.ui.base.BaseApp;

public class ViewUtil extends BaseUtils {
	private static LayoutInflater inflater;

	/**
	 * 弹出提示框
	 * 
	 * @author liujiaqi
	 * @creation time 2014-4-18下午2:07:05
	 * @version
	 * @param context
	 * @param title
	 * @param message
	 */
	public static void showMessage(Context context, String title, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context)
				.setTitle(title).setMessage(message)
				.setPositiveButton("确定", null);
		builder.create().show();
	}


	/**
	 * 关闭键盘
	 * 
	 * @author liujiaqi
	 * @creation time 2014-4-18下午11:12:00
	 * @version
	 * @param context
	 */
	public static void closeKeyBoard(Activity activity) {
		InputMethodManager im = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		im.hideSoftInputFromWindow(activity.getCurrentFocus()
				.getApplicationWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * 创建视图
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2011-10-8 下午4:39:34
	 * @return
	 */
	public static View buildView(int layout) {
		return getInflater().inflate(layout, null);
	}

	/**
	 * 根据资源文件设置view的背景和可见
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年4月7日 上午10:13:44
	 */
	public static void setBackgourdResAndVisible(int resId, View view) {
		if (resId > 0) {
			setViewsVisible(view);
			view.setBackgroundResource(resId);
		} else {
			setViewsGone(view);
		}
	}

	/**
	 * 设置textView的值和可见
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年4月7日 上午10:15:36
	 */
	public static void setTextViewAndVisible(String content, TextView view) {
		if (ValueUtil.isStrNotEmpty(content)) {
			setViewsVisible(view);
			view.setText(content);
		} else {
			setViewsGone(view);
		}
	}

	/**
	 * 设置view的可见状态
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-3-20 上午10:24:14
	 * @param view
	 * @param viewVisibleState
	 */
	public static void setViewVisible(View view, int viewVisibleState) {
		if (null == view) {
			return;
		}
		if (view.getVisibility() != viewVisibleState) {
			view.setVisibility(viewVisibleState);
		}
	}

	/**
	 * 设置views的不可见
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-3-20 上午10:24:14
	 * @param view
	 * @param viewVisibleState
	 */
	public static void setViewsGone(View... views) {
		if (null == views || views.length <= 0) {
			return;
		}
		for (View eachView : views) {
			setViewVisible(eachView, View.GONE);
		}
	}

	/**
	 * 设置views的可见
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-3-20 上午10:24:14
	 * @param view
	 * @param viewVisibleState
	 */
	public static void setViewsVisible(View... views) {
		if (null == views || views.length <= 0) {
			return;
		}
		for (View eachView : views) {
			setViewVisible(eachView, View.VISIBLE);
		}
	}

	/**
	 * 设置views的存在但不可见可见
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-3-20 上午10:24:14
	 * @param view
	 * @param viewVisibleState
	 */
	public static void setViewsInVisible(View... views) {
		if (null == views || views.length <= 0) {
			return;
		}
		for (View eachView : views) {
			setViewVisible(eachView, View.INVISIBLE);
		}
	}

	/**
	 * 创建视图
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2011-10-8 下午4:39:34
	 * @return
	 */
	public static View buildView(int layout, ViewGroup parentView) {
		return getInflater().inflate(layout, parentView);
	}

	/**
	 * 创建一个listview里的view,并且可以设置高度
	 * 
	 * @author carlos E-mail:carloscyy@gmail.com
	 * @version 创建时间：2011-11-17 上午11:07:06
	 * @param layoutId
	 * @param heightDip
	 * @return
	 */
	public static View buildListViewChild(int layoutId, int heightDip) {
		View view = buildView(layoutId);
		if (heightDip > 0) {
			int height = ViewUtil.dip2px(heightDip);
			ListView.LayoutParams lp = new ListView.LayoutParams(
					ListView.LayoutParams.MATCH_PARENT, height);
			view.setLayoutParams(lp);
		}
		return view;
	}

	/**
	 * 创建一个listview里的view,并且可以设置高度
	 * 
	 * @author carlos E-mail:carloscyy@gmail.com
	 * @version 创建时间：2011-11-17 上午11:07:06
	 * @param layoutId
	 * @param heightDip
	 * @return
	 */
	public static View buildListViewChildByPxHeight(int layoutId, int heightPx) {
		View view = buildView(layoutId);
		ListView.LayoutParams lp = new ListView.LayoutParams(
				ListView.LayoutParams.MATCH_PARENT, heightPx);
		view.setLayoutParams(lp);
		return view;
	}

	/**
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2011-10-8 下午4:40:36
	 * @return
	 */
	private static LayoutInflater getInflater() {
		if (null == inflater) {
			inflater = LayoutInflater.from(BaseApp.AppContext);
		}
		return inflater;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(float dpValue) {
		final float scale = BaseAC.currentActivity.getResources()
				.getDisplayMetrics().density;
		int result = (int) (dpValue * scale + 0.5f);
		return result;

	}


	/**
	 * 根据demin的resId来设置尺度
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-8-7 下午5:35:13
	 * @param bookrackItem
	 * @param dimenResId
	 * @return
	 */
	public static View buildListViewChildByDimen(int layoutId, int dimenResId) {
		int px = (int) BaseAC.currentActivity.getResources().getDimension(
				dimenResId);
		return buildListViewChildByPxHeight(layoutId, px);
	}

	/**
	 * 创建进度条，并显示
	 * 
	 * @param resId
	 * @return
	 */
	public static ProgressDialog showProdgressDialog(int resId) {
		return ProgressDialog.show(BaseAC.currentActivity, "",
				BaseAC.currentActivity.getString(resId), true);
	}

	/**
	 * 显示吐司内容
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2011-8-9 下午6:10:08
	 * @param msg
	 * @param isLongTime
	 */
	public static void showToast(String msg, boolean isLongTime) {
		if (BaseAC.currentActivity == null) {
			return;
		}
		int duration = Toast.LENGTH_SHORT;
		if (isLongTime) {
			duration = Toast.LENGTH_LONG;
		}
		Toast.makeText(BaseAC.currentActivity, msg, duration).show();
	}

	/**
	 * 显示吐司内容
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2011-8-9 下午6:10:08
	 * @param msg
	 * @param isLongTime
	 */
	public static void showToast(int resID) {
		showToast(ValueUtil.getString(resID), false);
	}

	/**
	 * 填充数字
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-25 下午3:54:04
	 * @param tvDownloadMsg
	 * @param downloadingNumber
	 */
	public static void fillNumberText(TextView tvNumber, int number) {
		if (null == tvNumber) {
			return;
		}

		if (number > 0) {
			String numberStr = "";
			if (number > 99) {
				numberStr = "99+";
			} else {
				numberStr += "" + number + "";
			}
			tvNumber.setText(numberStr);
			tvNumber.setVisibility(View.VISIBLE);
		} else {
			tvNumber.setVisibility(View.GONE);
			tvNumber.setText("");
		}

	}

	/**
	 * 创建弹出框
	 * 
	 * @param titielResId
	 * @param leftBtnNameResId
	 * @param rightBtnNameResId
	 * @param leftListener
	 * @param rightListener
	 */
	public static void alert(int titielResId, int leftBtnNameResId,
			int rightBtnNameResId,
			DialogInterface.OnClickListener leftListener,
			DialogInterface.OnClickListener rightListener) {
		alert(BaseAC.currentActivity, titielResId, leftBtnNameResId,
				rightBtnNameResId, leftListener, rightListener);
	}

	public static void alert(int msgId,
			DialogInterface.OnClickListener leftClickListener) {
		ViewUtil.alert(msgId, R.string.confirm, R.string.cancel,
				leftClickListener, null);
	}

	/**
	 * 创建弹出框
	 * 
	 * @param titielResId
	 * @param leftBtnNameResId
	 * @param rightBtnNameResId
	 * @param leftListener
	 * @param rightListener
	 */
	public static void alert(Context mContext, int titielResId,
			int leftBtnNameResId, int rightBtnNameResId,
			DialogInterface.OnClickListener leftListener,
			DialogInterface.OnClickListener rightListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(R.string.app_name);
		builder.setMessage(titielResId);
		builder.setCancelable(false);
		if (null == rightListener) {
			builder.setNegativeButton(rightBtnNameResId,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							dialog = null;
						}
					});
		} else {
			builder.setNegativeButton(rightBtnNameResId, rightListener);
		}
		builder.setPositiveButton(leftBtnNameResId, leftListener);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * 传入容器,id,和内容,辅助赋值
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年5月28日 下午1:30:35
	 * @param view
	 * @param tvcontent
	 * @param postFont1
	 */
	public static void setTextWitSuperView(View supView, int tvId,
			String content) {
		if (null == supView || ValueUtil.isStrEmpty(content)) {
			return;
		}
		TextView tv = (TextView) supView.findViewById(tvId);
		if (null == tv) {
			return;
		}
		tv.setText(content);
	}

}